<template>
  <div class="party-review">
    <div class="page-header">
      <h1>党建审核</h1>
    </div>

    <el-tabs v-model="active" @tab-change="refresh">
      <el-tab-pane label="组织生活" name="activities">
        <el-card>
          <el-table :data="active==='activities'?rows:[]" stripe v-loading="loading.activities">
            <el-table-column prop="activitySubject" label="主题" min-width="240" />
            <el-table-column prop="activityType" label="类型" width="120" />
            <el-table-column prop="status" label="状态" width="110">
              <template #default="{ row }"><el-tag :type="statusTagType(row.status)">{{ statusText(row.status) }}</el-tag></template>
            </el-table-column>
            <el-table-column label="操作" width="220" fixed="right">
              <template #default="{ row }">
                <el-button type="text" @click="openReview(row)">审核</el-button>
                <el-button type="text" @click="view(row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>

      <el-tab-pane label="理论学习" name="theory">
        <el-card>
          <el-table :data="active==='theory'?rows:[]" stripe v-loading="loading.theory">
            <el-table-column prop="studyTopic" label="学习主题" min-width="240" />
            <el-table-column prop="studyForm" label="形式" width="120" />
            <el-table-column prop="status" label="状态" width="110">
              <template #default="{ row }"><el-tag :type="statusTagType(row.status)">{{ statusText(row.status) }}</el-tag></template>
            </el-table-column>
            <el-table-column label="操作" width="220" fixed="right">
              <template #default="{ row }">
                <el-button type="text" @click="openReview(row)">审核</el-button>
                <el-button type="text" @click="view(row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>

      <el-tab-pane label="志愿服务" name="volunteer">
        <el-card>
          <el-table :data="active==='volunteer'?rows:[]" stripe v-loading="loading.volunteer">
            <el-table-column prop="serviceTitle" label="服务主题" min-width="240" />
            <el-table-column prop="serviceType" label="类型" width="120" />
            <el-table-column prop="status" label="状态" width="110">
              <template #default="{ row }"><el-tag :type="statusTagType(row.status)">{{ statusText(row.status) }}</el-tag></template>
            </el-table-column>
            <el-table-column label="操作" width="220" fixed="right">
              <template #default="{ row }">
                <el-button type="text" @click="openReview(row)">审核</el-button>
                <el-button type="text" @click="view(row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>
    </el-tabs>

    <el-dialog v-model="showReviewDialog" title="审核" width="520px">
      <el-form :model="reviewForm" label-width="96px">
        <el-form-item label="审核结果" required>
          <el-radio-group v-model="reviewForm.status">
            <el-radio label="APPROVED">通过</el-radio>
            <el-radio label="REJECTED">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input v-model="reviewForm.opinion" type="textarea" :rows="4" maxlength="1000" show-word-limit />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showReviewDialog=false">取消</el-button>
        <el-button type="primary" :loading="reviewLoading" @click="submitReview">提交</el-button>
      </template>
    </el-dialog>

  <!-- 详情对话框 -->
  <el-dialog v-model="showDetail" title="详情" width="680px">
    <div v-if="current">
      <el-descriptions :column="2" border v-if="active==='activities'">
        <el-descriptions-item label="主题">{{ current.activitySubject }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ current.activityType }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ statusText(current.status) }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions :column="2" border v-else-if="active==='theory'">
        <el-descriptions-item label="学习主题">{{ current.studyTopic }}</el-descriptions-item>
        <el-descriptions-item label="形式">{{ current.studyForm }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ statusText(current.status) }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions :column="2" border v-else>
        <el-descriptions-item label="服务主题">{{ current.serviceTitle }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ current.serviceType }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ statusText(current.status) }}</el-descriptions-item>
      </el-descriptions>
    </div>
    <template #footer>
      <el-button type="primary" @click="showDetail=false">关闭</el-button>
    </template>
  </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { listActivities, reviewActivity } from '@/api/party'
import { getAllStudies, reviewStudy } from '@/api/theory'
import { getAllVolunteers, reviewVolunteer } from '@/api/volunteer'

const active = ref('activities')
const rows = ref([])
const loading = reactive({ activities: false, theory: false, volunteer: false })

const showReviewDialog = ref(false)
const reviewLoading = ref(false)
const currentId = ref(null)
const reviewForm = ref({ status: '', opinion: '' })
const showDetail = ref(false)
const current = ref(null)

function statusText(s){ return ({ PENDING: '待审核', APPROVED: '已通过', REJECTED: '已驳回' }[s] || '待审核') }
function statusTagType(s){ return ({ PENDING: 'warning', APPROVED: 'success', REJECTED: 'danger' }[s] || 'info') }

async function refresh(){
  if(active.value==='activities') await loadActivities()
  else if(active.value==='theory') await loadStudies()
  else await loadVolunteers()
}

async function loadActivities(){ loading.activities=true; try{ const res = await listActivities({}); rows.value = res?.data || res || [] } finally { loading.activities=false } }
async function loadStudies(){ loading.theory=true; try{ const res = await getAllStudies({}); rows.value = res?.data || res || [] } finally { loading.theory=false } }
async function loadVolunteers(){ loading.volunteer=true; try{ const res = await getAllVolunteers({ status:'PENDING' }); rows.value = res?.data || res || [] } finally { loading.volunteer=false } }

function openReview(row){ currentId.value = row.id; reviewForm.value = { status:'', opinion:'' }; showReviewDialog.value = true }
async function submitReview(){
  if(!reviewForm.value.status){ ElMessage.warning('请选择审核结果'); return }
  reviewLoading.value = true
  try{
    if(active.value==='activities') await reviewActivity(currentId.value, reviewForm.value)
    else if(active.value==='theory') await reviewStudy(currentId.value, reviewForm.value)
    else await reviewVolunteer(currentId.value, reviewForm.value)
    ElMessage.success('审核完成')
    showReviewDialog.value = false
    await refresh()
  } finally { reviewLoading.value=false }
}

function view(row){ current.value = row; showDetail.value = true }

onMounted(refresh)
</script>

<style scoped>
.party-review { max-width: 1400px; }
.page-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:16px; }
.filters { display:flex; align-items:center; }
</style>


